package com.tcoiss.log.controller;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.component.api.model.ApiLogVo;
import com.tcoiss.log.domain.ApiLog;
import com.tcoiss.log.service.IApiLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import com.tcoiss.common.core.web.page.TableDataInfo;

/**
 * 接口调用日志Controller
 * 
 * @author zw
 * @date 2021-05-07
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/apiLog" )
public class ApiLogController extends BaseController {

    private final IApiLogService iCallApiLogService;

    /**
     * 查询接口调用日志列表
     */
    @PreAuthorize(hasPermi = "apiLog:log:list")
    @GetMapping("/list")
    public TableDataInfo list(ApiLog apiLog) {
        startPage();
        List<ApiLog> list = iCallApiLogService.queryList(apiLog);
        return getDataTable(list);
    }

    /**
     * 导出接口调用日志列表
     */
    @PreAuthorize(hasPermi = "apiLog:log:export" )
    //@Log(title = "接口调用日志" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApiLog apiLog) throws IOException
    {
        List<ApiLog> list = iCallApiLogService.queryList(apiLog);
        ExcelUtil<ApiLog> util = new ExcelUtil<>(ApiLog.class);
        util.exportExcel(response,list, "log" );
    }

    /**
     * 获取接口调用日志详细信息
     */
    @PreAuthorize(hasPermi = "apiLog:log:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCallApiLogService.getById(id));
    }

    /**
     * 新增接口调用日志
     */
    //@PreAuthorize(hasPermi = "apiLog:log:add" )
    //@Log(title = "接口调用日志" , businessType = BusinessType.INSERT)
    @PostMapping("/saveApiLog")
    public R<Boolean> saveApiLog(@RequestBody ApiLogVo apiLog) {
        String json = JSON.toJSONString(apiLog);
        return R.ok(iCallApiLogService.save(JSON.parseObject(json,ApiLog.class)));
    }

    /**
     * 修改接口调用日志
     */
    @PreAuthorize(hasPermi = "apiLog:log:edit" )
    @Log(title = "接口调用日志" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApiLog apiLog) {
        return toAjax(iCallApiLogService.updateById(apiLog) ? 1 : 0);
    }

    /**
     * 删除接口调用日志
     */
    /*@PreAuthorize(hasPermi = "apiLog:log:remove" )
    @Log(title = "接口调用日志" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCallApiLogService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }*/

    @PreAuthorize(hasPermi = "apiLog:log:remove")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @GetMapping("/clean")
    public AjaxResult clean(ApiLog apiLog) {
        List<ApiLog> apiLogs = iCallApiLogService.queryList(apiLog);
        List<Long> ids = apiLogs.stream().map(ApiLog::getId).collect(Collectors.toList());
        return toAjax(iCallApiLogService.removeByIds(ids) ? 1 : 0);
    }
}
